/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VideoStreaming;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import net.jxta.j2me.Element;
import net.jxta.j2me.Message;
import net.jxta.j2me.PeerNetwork;

/**
 *
 * @author naveen
 */
public class Peers {

    private Hashtable PeerList = new Hashtable();
    private Vector VideoPeers = new Vector();
   // private FileSharing fsobj = new FileSharing();
    private PeerNetwork peerobj = null;
    private String Name; //this peer's name
    private String currentbuddy = null;

    public String getCurrentbuddy() {
        return currentbuddy;
    }

    public void setCurrentbuddy(String currentbuddy) {
        this.currentbuddy = currentbuddy;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }
    private String Pipeid; // peer to be requested's pipe id


    public Vector getVideoPeers() {
        return VideoPeers;
    }

    public void setVideoPeers(Vector VideoPeers) {
        this.VideoPeers = VideoPeers;
    }


    public Hashtable getPeerList() {
        return PeerList;
    }

    public void setPeerList(Hashtable PeerList) {
        this.PeerList = PeerList;
    }

    public String getPipeid() {
        return Pipeid;
    }

    public void setPipeid(String Pipeid) {
        this.Pipeid = Pipeid;
    }
    private boolean FileFound = false;

    public boolean isFileFound() {
        return FileFound;
    }

    public void setFileFound(boolean FileFound) {
        this.FileFound = FileFound;
    }

    public Peers() {
        setName("Naveen");
    }

   

    // searching for a file in a list of peers obtained
    public Message FileSearchReq(String filereq, String ReqPeer) {
        System.out.println("Peers ---> filesearchrequest");
        int i = 0;
        Element[] elmobj = new Element[3];
        elmobj[0] = new Element("ReqPeerName", ReqPeer.getBytes(), null, null);
        elmobj[1] = new Element("ReqFileName", filereq.getBytes(), null, null);
        elmobj[2] = new Element("GrpName", "NetPeerGroup".getBytes(),null,null);
        //creating a message
        Message msg = new Message(elmobj);

        //  while (!PeerList.isEmpty()) {
        //SentPeer = (String) PeerList.elementAt(i);
    /*    for (Enumeration en = PeerList.elements(); en.hasMoreElements();) {

            try {                
                String pipeId = (String)en.nextElement();
                peerobj.send(pipeId, msg);                
            } catch (Exception ex) {
                System.out.println("CANNOT SEND MESSAGE!!!");
                return false;
            }
        }
    return true;*/
        return msg;
    }

    // searching for the file within the share list
    public boolean Search(String FileReq, FileSharing fsobj) {
        System.out.println("in Peers.Search");
        //while (!fsobj.getFileList().isEmpty()) {
            for (int i = 0; i < fsobj.getFileList().size(); i++) {
                if(FileReq.equalsIgnoreCase((String)fsobj.getFileList().elementAt(i)))
                {System.out.println("filefound!!!!!!!");
                return true;       }
          // }
        }
        return false;
    }

    //removing buddy from the hashtable
    public void delBuddys(String budName){
        PeerList.remove(budName);
}
    //addding buddys and their ids to hashtable
    public void addBuddys(String budName,String budID){
        if(!PeerList.containsKey(budName)){
            PeerList.put(budName, budID);
}
    }
}
